package com.java1234.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.java1234.dao.ILibrarianDao;
import com.java1234.dao.IUserDao;
import com.java1234.dao.impl.LibrarianImpl;
import com.java1234.dao.impl.UserImpl;
import com.java1234.model.Librarian;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

public class LibrarianAddInterFrm extends JInternalFrame {
	private JTextField librarianAccountTxt;
	private JTextField librarianNameTxt;

	private DbUtil dbUtil = new DbUtil();
	private ILibrarianDao librarianDao = new LibrarianImpl();
	private IUserDao userDao = new UserImpl();
	private JTextField passwordTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LibrarianAddInterFrm frame = new LibrarianAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LibrarianAddInterFrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("图书管理员添加");
		setBounds(100, 100, 1016, 754);

		JLabel lblNewLabel = new JLabel("图书管理员账号：");

		JLabel lblNewLabel_1 = new JLabel("图书管理员姓名：");

		librarianAccountTxt = new JTextField();
		librarianAccountTxt.setColumns(10);

		librarianNameTxt = new JTextField();
		librarianNameTxt.setColumns(10);

		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				librarianAddActionPerformed(e);
			}
		});

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});

		JLabel lblNewLabel_2 = new JLabel("密码：");

		passwordTxt = new JTextField();
		passwordTxt.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(177).addComponent(lblNewLabel).addGap(19)
						.addComponent(librarianAccountTxt, GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE).addGap(447))
				.addGroup(groupLayout.createSequentialGroup()
						.addGroup(groupLayout
								.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(174).addComponent(lblNewLabel_1))
								.addGroup(groupLayout
										.createSequentialGroup().addGap(190).addGroup(
												groupLayout.createParallelGroup(Alignment.LEADING)
														.addComponent(btnNewButton).addComponent(lblNewLabel_2))))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(20)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addComponent(passwordTxt, GroupLayout.DEFAULT_SIZE, 239,
														Short.MAX_VALUE)
												.addComponent(librarianNameTxt, 239, 239, 239))
										.addGap(447))
								.addGroup(groupLayout.createSequentialGroup().addGap(38).addComponent(btnNewButton_1)
										.addContainerGap()))));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addContainerGap(218, Short.MAX_VALUE)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, groupLayout
						.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel).addGap(68)
										.addComponent(lblNewLabel_1).addGap(87))
								.addGroup(groupLayout.createSequentialGroup()
										.addComponent(librarianNameTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addGap(82)))
						.addGap(60))
						.addGroup(Alignment.TRAILING,
								groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(lblNewLabel_2).addComponent(passwordTxt,
														GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
										.addGap(54)))
				.addGap(26)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
				.addGap(193))
				.addGroup(groupLayout
						.createSequentialGroup().addGap(217).addComponent(librarianAccountTxt,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(477, Short.MAX_VALUE)));
		getContentPane().setLayout(groupLayout);

	}

	/**
	 * 图书管理员添加事件处理
	 * 
	 * @param e
	 */
	private void librarianAddActionPerformed(ActionEvent evt) {
		String librarianAccount = this.librarianAccountTxt.getText();
		String librarianName = this.librarianNameTxt.getText();
		String password = this.passwordTxt.getText();
		if (StringUtil.isEmpty(librarianAccount)) {
			JOptionPane.showMessageDialog(null, "图书管理员账号不能为空！");
			return;
		}
		if (StringUtil.isEmpty(librarianName)) {
			JOptionPane.showMessageDialog(null, "图书管理员姓名不能为空！");
			return;
		}
		Librarian librarian = new Librarian(librarianAccount, librarianName, password);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int m = librarianDao.add(con, librarian);
			int n = userDao.addLibrarian(con, librarian);
			if (m == 1 && n == 1) {
				JOptionPane.showMessageDialog(null, "图书管理员添加成功！");
				resetValue();
			} else {
				JOptionPane.showMessageDialog(null, "图书管理员添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "图书管理员添加失败！");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 重置事件处理
	 * 
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		this.resetValue();

	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.librarianAccountTxt.setText("");
		this.librarianNameTxt.setText("");
		this.passwordTxt.setText("");
	}
}
